<?php
if ( phpversion() >= "4.2.0")
{  
    extract($_POST);  
    extract($_GET);  
    extract($_SERVER);  
} 

        if( !ini_get('safe_mode') ){
            set_time_limit(240);
        } 
		else
		{
		echo "warning: safe mode is on cat't set time limit<br>";
		}
		
//include_only();
require_once "include/config.php";
require_once "include/functions.php";
require_once "include/srt_converter.php";

//$video_id = $_POST["video_id"];
if(!empty($video_id) and substr($video_id,0,10)=="favourites")
{
$is_favourite_video = '1';
$video_id = str_replace('favourites','',$video_id);
$fav_video = 'yes';
}
else
{
$is_favourite_video = '0';
$fav_video = 'no';
}

$user_info = get_user_info();
$user_id = $user_info['id'];

$target_path = "tmp/";
$target_filename = getmicrotime() ;
$random_number = generate_random_number('1000000', '9999999');
$target_path = $target_path . $random_number . $target_filename . ".srt" ;
//$original_file_name = basename($_FILES['uploadedfile']['name']);
if($fav_video=='yes'){$video_file_info = get_favourite_video_information($video_id);} else {$video_file_info = get_video_information($video_id);}
$video_file_title = $video_file_info['name'];
$lang_name = get_language_by_id($new_lang);
$lang_name = $lang_name['name'];

if(empty($subtitle_name))
{
$title = $video_file_title . " " . $lang_name;
}
else
{
$title = $subtitle_name . " " . $lang_name;
}

$table_name = "subtitle_".$random_number.$user_id.$video_id;


if(!empty($upload_subtitle) and $upload_subtitle=='yes')
{
//uploading the file
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {

    mysql_query("INSERT INTO subtitles (video_id, user_id, table_name, admin, title, language, original_language, is_favourite_video)VALUES ('$video_id', '$user_id', '$table_name', '1', '$title', '$new_lang', '$original_lang', '$is_favourite_video')") ;

	create_subtitle_table($table_name);
	
	echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}


//import .srt file content into database
$srt_content = convert_srt($target_path);

//echo count($srt_content);
$counter = 0;
for ($i = 0; $i <= count($srt_content)- 1; $i++) {

$start = $srt_content[$i]['begin'];
$end = $srt_content[$i]['end'];
$text = $srt_content[$i]['text'];

$start = mysql_real_escape_string($start);
$end = mysql_real_escape_string($end);
$text = mysql_real_escape_string($text);

mysql_query("INSERT INTO `$table_name` (`id` ,`line_number` ,`start` ,`end` ,`text`) VALUES (NULL , '', '$start', '$end', '$text');	")or die("database failed with error: ".mysql_error());
}
//echo "<br>imported lines: ".$counter."<br>";

//rearrangeing line numbers
arrange_subtitle_lines_order($table_name);
}

//////////////////////////

if(!empty($create_subtitle) and $create_subtitle=='yes')
{

//writing .srt file to server
$myFile = $target_path;
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $subtitle_text;
fwrite($fh, $stringData);
fclose($fh);
//

    mysql_query("INSERT INTO subtitles (video_id, user_id, table_name, admin, title, language, original_language, is_favourite_video)VALUES ('$video_id', '$user_id', '$table_name', '1', '$title', '$new_lang', '$original_lang', '$is_favourite_video')");

	create_subtitle_table($table_name);

//import .srt file content into database
$srt_content = convert_srt($target_path);

for ($i = 0; $i <= count($srt_content)- 1; $i++) {

$start = $srt_content[$i]['begin'];
$end = $srt_content[$i]['end'];
$text = $srt_content[$i]['text'];

$start = mysql_real_escape_string($start);
$end = mysql_real_escape_string($end);
$text = mysql_real_escape_string($text);

mysql_query("INSERT INTO `$table_name` (`id` ,`line_number` ,`start` ,`end` ,`text`) VALUES (NULL , '', '$start', '$end', '$text');	");
}

//rearrangeing line numbers
arrange_subtitle_lines_order($table_name);
}

is_subtitle_empty($table_name);
unlink($target_path);

?>





<script type="text/javascript">
window.location = "index.php?page=my_subtitles";
</script>